/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ctrl;

import model.ViewBookingsBackingBean;
import entities.Booking;
import entities.GuestUser;
import hotel.jpa.BookingJpaCtrl;
import hotel.jpa.Database;
import hotel.jpa.IBookingJpaCtrl;
import hotel.jpa.IJpaCtrl;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author Viktor
 */
@ManagedBean(name="viewBookingsControllerBean")
@ApplicationScoped
public class ViewBookingsControllerBean {
    
    protected List<ViewBookingsBackingBean> bookings = new ArrayList<ViewBookingsBackingBean>();
    protected Booking booking;
    
    public List<ViewBookingsBackingBean> getBookings(){
        
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("hotel_pu");
        IBookingJpaCtrl bookingCtrl = new BookingJpaCtrl(null, emf);
      
        bookings = bookingCtrl.findEntities();

        return bookings;
    }

    public Booking getBooking() {
        return booking;
    }

    public void setBooking(Booking booking) {
        this.booking = booking;
    }
    
    public void setBookings(List<ViewBookingsBackingBean> b){
        this.bookings = b;
    }
    
    public String del(GuestUser user, Date startDate){
      
        List<Booking> bs = Database.getBookingController().findEntities();
        
        Booking newBooking = null;
        for(Booking b : bs){
            if(b.getUser().getName().compareTo(user.getName()) == 0 && b.getStartDate().compareTo(startDate) == 0){
                newBooking = b;
            }
        }
        Database.getBookingController().destroy(newBooking.getId());
       
        return "/admin/booking_edit.xhtml";
    }
    
    public String saveValue(Booking b){
        
        booking = b;
        return "/admin/booking_edit_step2.xhtml";
    }
    
    public String edit(Booking b){
        
        Database.getBookingController().edit(b);
        return "/admin/booking_edit.xhtml";
    }
}
